package com.zj.healthmanage.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zj.healthmanage.constant.RedisConstant;
import com.zj.healthmanage.entity.PageResult;
import com.zj.healthmanage.entity.QueryPageBean;
import com.zj.healthmanage.mapper.SetmealCheckgroupMapper;
import com.zj.healthmanage.pojo.SetmealCheckgroup;
import com.zj.healthmanage.utils.QiniuUtils;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.zj.healthmanage.mapper.SetmealMapper;
import com.zj.healthmanage.pojo.Setmeal;
import com.zj.healthmanage.service.SetmealService;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImpl implements SetmealService{

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private JedisPool jedisPool;

    @Resource
    private SetmealCheckgroupMapper setmealCheckgroupMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return setmealMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Setmeal record) {
        return setmealMapper.insert(record);
    }

    @Override
    public int insertSelective(Setmeal record,Integer[] checkgroupIdList) {
        Integer setmealId = record.getId();
        setSetmealAndCheckGroup(setmealId,checkgroupIdList);
        String fileName = record.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
        return setmealMapper.insertSelective(record);
    }

    @Override
    public Setmeal selectByPrimaryKey(Integer id) {
        return setmealMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Setmeal record,Integer[] checkgroupIdList) {
        Integer setmealId = record.getId();
        Setmeal setmeal = setmealMapper.selectByPrimaryKey(setmealId);
        String img = setmeal.getImg();
        if(record.getImg()!=null && !img.equals(record.getImg())){
            QiniuUtils.deleteFileFromQiniu(img);
        }
        setmealCheckgroupMapper.deleteSetMealAndCheckGroup(setmealId);
        setSetmealAndCheckGroup(setmealId,checkgroupIdList);
        return setmealMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Setmeal record) {
        return setmealMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult querySetMealByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealMapper.querySetMealByPage(queryString);
        long total = page.getTotal();
        List<Setmeal> rows = page.getResult();
        return new PageResult(total,rows);
    }

    private void setSetmealAndCheckGroup(Integer id,Integer[] checkgroupIdList) {
        if (id != null && checkgroupIdList.length > 0){
            for (Integer checkgroupId : checkgroupIdList) {
                SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
                setmealCheckgroup.setSetmealId(id);
                setmealCheckgroup.setCheckgroupId(checkgroupId);
                setmealCheckgroupMapper.insert(setmealCheckgroup);
            }
        }
    }

    @Override
    public List<Integer> queryCheckGroupIdsById(Integer id) {
        return setmealCheckgroupMapper.queryCheckGroupIdsById(id);
    }

    @Override
    public List<Map<String, Object>> querySetmealCount() {
        return setmealMapper.querySetmealCount();
    }

}
